package com.sensorsdata.webhook.processor.impl;

import com.alibaba.fastjson.JSON;
import com.sensorsdata.webhook.Bootstrap;
import com.sensorsdata.webhook.entry.SfWebhookEntry;
import com.sensorsdata.webhook.processor.SfWebhookProcessor;
import com.sensorsdata.webhook.server.EndpointServer;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
@Service
public class SfWebhookProcessorImpl  implements SfWebhookProcessor {
    private AtomicLong eventCounter = new AtomicLong(0);

    @Override
    public void handleWebhookEntries(List<SfWebhookEntry> events) {
        System.out.println(JSON.toJSONString(events));

        // 在这处理一批请求，并设置是否处理成功
        for (SfWebhookEntry webhookEvent : events) {
            long count = eventCounter.incrementAndGet();
            System.out.printf("webhook request [%d]: %s%n", count, webhookEvent.getRequest());

            if (count == 3) {
                webhookEvent.getResponse().failed("some reason");
            } else {
                webhookEvent.getResponse().succeeded();
            }
        }
    }
}
